//
//  MainViewController.m
//  我的微博
//
//  Created by tom-1 on 15/12/5.
//  Copyright © 2015年 tom-1. All rights reserved.
//

#import "MainViewController.h"
#import "HomeViewController.h"
#import "MessageViewController.h"
#import "DiscoverViewController.h"
#import "ProfileViewController.h"
#import "UIButton+Category.h"

@interface MainViewController ()
// 按钮

@property (nonatomic,strong) UIButton *addBtn;

@end

@implementation MainViewController
#pragma make - 懒加载控件
- (UIButton *)addBtn {
    if (_addBtn == nil) {
        _addBtn = [[UIButton alloc] initWithImageNameBackImageName:@"tabbar_compose_icon_add" backImageName:@"tabbar_compose_button_highlighted"];
        
    }
    return _addBtn;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 
    [self addChildViewControllers];
    [self setupAddBtn];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // 将钻屑按钮弄到最前面
    [self.tabBar bringSubviewToFront:self.addBtn];
}


#pragma mark - 设置界面的一些方法
// 设置撰写按钮
- (void)setupAddBtn {
    // 1.添加按钮
    [self.tabBar addSubview:self.addBtn];
    // 2.调整按钮
    NSInteger count = self.childViewControllers.count;
    CGFloat w = self.tabBar.bounds.size.width / count - 1;
    self.addBtn.frame = CGRectInset(self.tabBar.bounds, w * 2, 0);
    
    // 3.添加监听方法
    [self.addBtn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
}

// 添加控制器
- (void)addChildViewController:(UIViewController *)childController title:(NSString *)title imageName:(NSString *)imageName {
    // 设置标题
    childController.title = title;
    // 设置图像
    childController.tabBarItem.image = [UIImage imageNamed:imageName];
    childController.tabBarItem.badgeValue = @"5";
    
    // 导航控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:childController];
    
    [self addChildViewController:nav];
    
}
// 添加所有控制器
- (void)addChildViewControllers {
    self.tabBar.tintColor = [UIColor orangeColor];
    [self addChildViewController:[HomeViewController new] title:@"首页" imageName:@"tabbar_home"];
    [self addChildViewController:[MessageViewController new] title:@"消息" imageName:@"tabbar_message_center"];
    
    [self addChildViewController:[UIViewController new]];
    [self addChildViewController:[DiscoverViewController new] title:@"发现" imageName:@"tabbar_discover"];
    [self addChildViewController:[ProfileViewController new] title:@"我的" imageName:@"tabbar_profile"];
    
    
}
// 监听方法
- (void)click {
    NSLog(@"尼玛逼,点我干嘛");
}
@end
